/**
 *    Copyright (c) [2021] [白晓明]
 *    [IMHarmonyOSLib] is licensed under Mulan PSL v2.
 *    You can use this software according to the terms and conditions of the Mulan PSL v2.
 *    You may obtain a copy of Mulan PSL v2 at:
 *             http://license.coscl.org.cn/MulanPSL2
 *    THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 *    See the Mulan PSL v2 for more details.
 */
package com.itming.ui.uilib.bar;

import com.itming.ui.coptlib.core.IMIconButton;
import com.itming.ui.uilib.ResourceTable;
import com.itming.ui.uilib.util.Constant;
import ohos.agp.components.*;
import ohos.agp.utils.Color;
import ohos.app.Context;

import static ohos.agp.components.ComponentContainer.LayoutConfig.MATCH_PARENT;

/**
 * <p>自定义标题栏</p>
 * <ol>
 *     <li>左侧返回按钮</li>
 *
 *     <li>中间标题内容</li>
 *
 *     <li>
 *          <p>右侧图标按钮组</p>
 *          <ol>
 *              <li>右侧搜索按钮</li>
 *              <li>右侧多选按钮组</li>
 *          </ol>
 *     </li>
 * </ol>
 * @author itming
 * @date 2021-06-26
 */
public class IMCustomTitleBar extends DependentLayout {
    public IMCustomTitleBar(Context context) {
        this(context, null);
    }

    public IMCustomTitleBar(Context context, AttrSet attrSet) {
        this(context, attrSet, null);
    }

    public IMCustomTitleBar(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
        initImCustomTitleBar(context, attrSet);
    }

    /**
     * 初始化标题栏
     * @param attrSet
     */
    private void initImCustomTitleBar(Context context, AttrSet attrSet) {
//        ResourceManager resManager = getResourceManager();
        //动态加载Layout
        Component component = LayoutScatter.getInstance(context)
                .parse(ResourceTable.Layout_layout_title, null, false);

        //子组件
        IMIconButton backBtn = (IMIconButton) component.findComponentById(ResourceTable.Id_back_btn);
        Text titleBarContent = (Text) component.findComponentById(ResourceTable.Id_page_title);
        DirectionalLayout groupBtn = (DirectionalLayout) component.findComponentById(ResourceTable.Id_group_btn);
        IMIconButton searchBtn = (IMIconButton) groupBtn.findComponentById(ResourceTable.Id_search_btn);
        IMIconButton selectBtn = (IMIconButton) groupBtn.findComponentById(ResourceTable.Id_plus_btn);
        //添加layout到父组件
        addComponent(component);

        //设置标题栏背景色
        if (attrSet.getAttr("bar_bg").isPresent()) {
            component.setBackground(attrSet.getAttr("bar_bg").get().getElement());
        } else {
            component.setBackground(getBackgroundElement());
        }

        /**
         * 设置标题栏内容
         */
        if (attrSet.getAttr("bar_content").isPresent()) {
            titleBarContent.setText(attrSet.getAttr("bar_content").get().getStringValue());
        } else {
            titleBarContent.setText("");
        }

        //设置标题栏内容字体大小
        if (attrSet.getAttr("bar_content_size").isPresent()) {
            titleBarContent.setTextSize(attrSet.getAttr("bar_content_size").get().getIntegerValue(), Text.TextSizeType.FP);
        } else {
//            try {
//                titleBarContent.setTextSize(resManager.getElement(ResourceTable
//                        .Integer_default_title_bar_content_size).getInteger(),
//                        Text.TextSizeType.FP);
//            } catch (IOException | NotExistException | WrongTypeException e) {
//                e.printStackTrace();
//            }
            titleBarContent.setTextSize(Constant.DEFAULT_BAR_CONTENT_SIZE, Text.TextSizeType.FP);
        }

        //设置标题内容字体颜色
        if (attrSet.getAttr("bar_content_color").isPresent()) {
            titleBarContent.setTextColor(attrSet.getAttr("bar_content_color")
                    .get().getColorValue());
        } else {
            if (attrSet.getAttr("bar_bg").isPresent()) {
                titleBarContent.setTextColor(Color.WHITE);
            } else {
                titleBarContent.setTextColor(Color.BLACK);
            }
        }

        //设置是否显示返回按钮
        if (attrSet.getAttr("bar_back_btn").isPresent()) {
            if (attrSet.getAttr("bar_back_btn").get().getBoolValue()) {
                backBtn.setVisibility(VISIBLE);
            } else {
                backBtn.setVisibility(INVISIBLE);
            }
        } else {
            backBtn.setVisibility(INVISIBLE);
        }

        //设置返回按钮颜色
        if (attrSet.getAttr("bar_back_btn_color").isPresent()) {
            backBtn.setTextColor(attrSet.getAttr("bar_back_btn_color")
                    .get().getColorValue());
        } else {
            if (attrSet.getAttr("bar_bg").isPresent()) {
                backBtn.setTextColor(Color.WHITE);
            } else {
                backBtn.setTextColor(Color.BLACK);
            }
        }

        //设置右边按钮组可见值(其中之一可见，即容器可见)
        if (attrSet.getAttr("bar_search_btn").isPresent()
                || attrSet.getAttr("bar_select_btn").isPresent()) {
            if (attrSet.getAttr("bar_search_btn").isPresent()) {
                if (attrSet.getAttr("bar_search_btn").get().getBoolValue()) {
                    searchBtn.setVisibility(VISIBLE);
                } else {
                    searchBtn.setVisibility(HIDE);
                }
            } else {
                selectBtn.setWidth(MATCH_PARENT);
                searchBtn.setVisibility(HIDE);
            }
            if (attrSet.getAttr("bar_select_btn").isPresent()) {
                if (attrSet.getAttr("bar_select_btn").get().getBoolValue()) {
                    selectBtn.setVisibility(VISIBLE);
                } else {
                    selectBtn.setVisibility(HIDE);
                }
            } else {
                searchBtn.setWidth(MATCH_PARENT);
                selectBtn.setVisibility(HIDE);
            }
            groupBtn.setVisibility(VISIBLE);
        } else {
            groupBtn.setVisibility(HIDE);
        }

        //设置右侧按钮颜色
        if (attrSet.getAttr("bar_search_btn_color").isPresent()) {
            searchBtn.setTextColor(attrSet.getAttr("bar_search_btn_color")
                    .get().getColorValue());
        } else {
            if (attrSet.getAttr("bar_bg").isPresent()) {
                searchBtn.setTextColor(Color.WHITE);
            } else {
                searchBtn.setTextColor(Color.BLACK);
            }
        }
        if (attrSet.getAttr("bar_select_btn_color").isPresent()) {
            selectBtn.setTextColor(attrSet.getAttr("bar_select_btn_color")
                    .get().getColorValue());
        } else {
            if (attrSet.getAttr("bar_bg").isPresent()) {
                selectBtn.setTextColor(Color.WHITE);
            } else {
                selectBtn.setTextColor(Color.BLACK);
            }
        }
    }
}
